$("#isEduCounselor").click(function () {
    if (isEduCounselor()) {
        let toAppend = "<div class='input-group' id='temp-input'> " +
            "<input type=\"text\" id=\"major\" placeholder=\"请输入专业\"></div>"
        $(this).parent().parent().after(toAppend)
    } else {
        $('#temp-input').remove();
    }
})

function submit() {
    let feedbacks = ["手机号格式错误", "用户名由字母、数字、下划线6-16个字符组成",
        "邮箱格式错误", "密码由6-16个字符组成", "用户名/手机号/邮箱重复"];
    let i;
    const uPhoneIn = $('#uPhone').val();
    const PhoneRegex = /^1\d{10}/;

    let uIDIn = $("#uID").val();
    const IDRegex = /^[a-zA-Z0-9_\u4e00-\u9fa5]{0,16}$/

    const uEmailIn = $("#uEmail").val();
    const EmailRegex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/

    const uPasswordIn = $("#uPassword").val();
    const PasswordRegex = /^.{6,16}$/

    const errorMessages = $("span[class~='error-message']");
    if (!$("input[type='checkbox']").is(":checked")) {
        if ($("#bottom-text2").length === 0) {
            $("button[type='submit']").after("<sapn style='color: #ff0000;' id='bottom-text2'>请阅读并同意《用户服务协议》和《隐私政策》</sapn>")
        }
        return;
    } else {
        $('#bottom-text2').text("");
    }
    if (uPhoneIn.match(PhoneRegex)) {
        errorMessages[0].innerHTML = "";
        errorMessages[0].classList.remove("reg-feedback");
    } else {
        errorMessages[0].innerHTML = feedbacks[0];
        errorMessages[0].classList.add("reg-feedback");
        return;
    }
    if (uIDIn.match(IDRegex)) {
        errorMessages[1].innerHTML = "";
        errorMessages[1].classList.remove("reg-feedback");
    } else {
        errorMessages[1].innerHTML = feedbacks[1];
        errorMessages[1].classList.add("reg-feedback");
        return;
    }
    if (uEmailIn.match(EmailRegex)) {
        errorMessages[2].innerHTML = "";
        errorMessages[2].classList.remove("reg-feedback");
    } else {
        errorMessages[2].innerHTML = feedbacks[2];
        errorMessages[2].classList.add("reg-feedback");
        return;
    }
    if (uPasswordIn.match(PasswordRegex)) {
        errorMessages[3].innerHTML = "";
        errorMessages[3].classList.remove("reg-feedback");
    } else {
        errorMessages[3].innerHTML = feedbacks[3];
        errorMessages[3].classList.add("reg-feedback");
        return;
    }
    if (isEduCounselor()) {
        const major = $('#major').val();
        eduCounselorRegister(uIDIn,uPasswordIn,uEmailIn,uPhoneIn,major)
    }else {
        studentRegister(uIDIn,uEmailIn,uPasswordIn,uPhoneIn)
    }
}

function isEduCounselor() {
    return $('#isEduCounselor').is(":checked");
}

function studentRegister(uID, uEmail, uPassword, uPhone) {
    let uIDIn = $("#uID").val();
    $.ajax({
            url: gOptions.defaultURLPrefix+"/yewen/account/studentRegister",
            data: {
                "id": uID,
                "email": uEmail,
                "password": uPassword,
                "tel": uPhone
            },
            type: "POST",
            dataType: "json"
        }
    ).done(function (data) {
        if (data.code === 0) {
            location.replace(gOptions.defaultURLPrefix+"/yewen/pages/home?id=" + uIDIn);
        } else {
            alert(data.message);
        }
    }).fail(function (xhr, status, errorThrown) {
        alert("服务器在开小差...");
        console.log("Error: " + errorThrown);
    })
}
function eduCounselorRegister(uID,uPassword,uEmail,uPhone,major) {
    if (major===""){
        alert("请填写专业")
        return;
    }
    $.ajax({
        url:gOptions.defaultURLPrefix+"/yewen/account/educounselorRegister",
        type: "post",
        data:{
            "id":uID,
            "password":uPassword,
            "tel":uPhone,
            "email":uEmail,
            "major":major,
            "state":true
        },
        // contentType:"application/json;charset=utf-8",
        dataType: "json"
    }).done(function (data) {
        console.log(data)
        if (data.code===0){
            location.replace(gOptions.defaultURLPrefix+"/yewen/pages/home?id="+uID);
        }else {
            alert(data.message)
        }
    })
}